/**
 * Copyright by ChenLiPeng 2018
 * Date:2018年4月25日下午5:56:09
 */
package com.efast.dmom.mgmt.service.les.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mgmt.entity.master.TmBasPackageSn;
import com.efast.dmom.mgmt.entity.mes.TeProductInbound;
import com.efast.dmom.mgmt.service.les.FinishedProductJobService;
import com.efast.dmom.mgmt.service.les.TwDocAsnDetailsService;
import com.efast.dmom.mgmt.service.les.TwDocAsnHeaderService;
import com.efast.dmom.mgmt.service.master.TmBasPackageSnService;
import com.efast.dmom.mgmt.service.mes.TeProductInboundService;
import com.efast.dmom.mgmt.utils.Assist;

/**
 * @author Administrator
 *
 */

@Service
public class FinishedProductJobServiceImpl implements FinishedProductJobService {
	
	// 成品入库信息中间表 TE_PRODUCT_INBOUND
	@Autowired
	private TeProductInboundService teProductInboundService;
	
	private TwDocAsnDetailsService twDocAsnDetailsService;
	
	private TwDocAsnHeaderService twDocAsnHeaderService;
	
	private TmBasPackageSnService tmBasPackageSnService;

	@Override
	public void excute() {


		// 【1】：查询成品入库信息中间表新增的数据，并且按照订单号分组,同一个订单号的新建一个asn header记录
		List<TeProductInbound> listTeProductInbound = teProductInboundService.selectNewTeProductInbound();
		if (listTeProductInbound.size()==0) {
			System.out.println("没有待入库的成品................................");
		}
		for (int i = 0; i < listTeProductInbound.size(); i++) {
			TeProductInbound teProductInbound = listTeProductInbound.get(i);
			int twDocAsnHeaderId = twDocAsnHeaderService.insertTwDocAsnHeaderByJob(teProductInbound);
			twDocAsnDetailsService.insertNonEmptyTwDocAsnDetailsByJob(teProductInbound, twDocAsnHeaderId);
			/* int twDocAsnHeaderId = 0;
			   if (i == 0) {
			    
				System.out.println(1111111);
				twDocAsnHeaderId = twDocAsnHeaderService.insertTwDocAsnHeaderByJob(teProductInbound);
				twDocAsnDetailsService.insertNonEmptyTwDocAsnDetailsByJob(teProductInbound, twDocAsnHeaderId);
			} else {
				if (teProductInbound.getSoNo().equals(listTeProductInbound.get(i - 1).getSoNo())) {
					System.out.println(2222222);
					twDocAsnDetailsService.insertNonEmptyTwDocAsnDetailsByJob(teProductInbound, twDocAsnHeaderId);
				} else {
					System.out.println(33333333);
					twDocAsnHeaderId = twDocAsnHeaderService.insertTwDocAsnHeaderByJob(teProductInbound);
					twDocAsnDetailsService.insertNonEmptyTwDocAsnDetailsByJob(teProductInbound, twDocAsnHeaderId);
				}
			}*/

			// 【2】：更新 成品入库信息中间表 TE_PRODUCT_INBOUND 为成功状态
			teProductInbound.setStatus("1"); // 状态 0：新数据 1：成功 2：失败
			teProductInboundService.updateNonEmptyTeProductInboundById(teProductInbound);

			// 【3】：更新 包装序号及包装信息 tm_bas_package_sn 为 已经入库
			TmBasPackageSn tmBasPackageSn = new TmBasPackageSn();
			tmBasPackageSn.setIsInbound(1); // 是否入库，0：否，1：是
			tmBasPackageSnService.updateNonEmptyTmBasPackageSn(tmBasPackageSn,
					new Assist(Assist.andEq("package_sn", teProductInbound.getPackSerialNo())));
		}
	}
}
